package com.bydq.patrolsystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bydq.patrolsystem.annotation.MyLog;
import com.bydq.patrolsystem.entity.PsRole;
import com.bydq.patrolsystem.entity.PsUser;
import com.bydq.patrolsystem.service.PsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private PsRoleService psRoleService;

//    @GetMapping("/roleList")
//    public String role(Model model){
//
//        //每页显示数量
//        int pageSize = 5;
//
//        //开始分页
//        model.addAttribute("page" , psRoleService.selectPageByEnditions(1,pageSize,"",""));
//
//        return "roleList";
//    }
    @GetMapping("/roleList")
    public String toRole(){
        return "roleList";
    }

    @MyLog("查看所有角色")
    @GetMapping("/roleListData")
    @ResponseBody
    public IPage<PsRole> role(@RequestParam(value = "currentPage" , required = false , defaultValue = "1")
                               Integer currentPage,
                              @RequestParam(value = "pageSize" , required = false , defaultValue = "10")
                                Integer pageSize,
                              @RequestParam(value = "roleName" ,required = false , defaultValue = "")
                               String roleName,
                              @RequestParam(value = "isStart" ,required = false , defaultValue = "")
                               String isStart
                           ){
        //每页显示数量
        //开始分页
        return psRoleService.selectPageByEnditions(currentPage,pageSize,roleName,isStart);
    }

    @MyLog("改变角色状态")
    @PostMapping("/setStart/{id}/{start}")
    @ResponseBody
    public boolean setStart(@PathVariable("id") Integer id ,
                            @PathVariable("start") Integer start){

        //开始设置对应的状态 返回是否成功
        return psRoleService.setStart(id,start);
    }


    @GetMapping("/addRole")
    public String toAddRole(){
        return "addRole";
    }

    @GetMapping("/updateRole/{id}")
    public String toUpdate(@PathVariable Integer id,Model model) {
        model.addAttribute("role",psRoleService.getById(id));
        return "roleUpdate";
    }


    @MyLog("添加或修改角色信息")
    @PostMapping("/addOrUpdate")
    @ResponseBody
    public boolean addOrUpdate(PsRole role, HttpSession session){
        //设置当前用户
        role.setCreatedBy(((PsUser)session.getAttribute("user")).getUserCode());
        return psRoleService.saveOrUpdate(role);
    }

    @MyLog("删除一条角色记录")
    @PostMapping("/del/{id}")
    @ResponseBody
    public boolean del(@PathVariable Integer id){
        try{

            return psRoleService.removeById(id);
        }catch (Exception e){
            return false;
        }
    }

}
